package validate

import (
	"regexp"
	"strconv"
	"strings"
	"time"
	"reflect"
	//"fmt"
)

func CheckData(data map[string]string,validateArr map[string]string,scene string) string{
	//data := parms.(map[string]string)
	sceneArr:=strings.Split(scene,"|")  //应用场景
	err:=""
	keyName := ""
	sceneStatus := false
I:
	for key, str := range validateArr {
		//取出别名
		keyArr := strings.Split(key,":")
		if len(keyArr) == 2{
			keyName = keyArr[1]
			key = keyArr[0]
		}else{
			keyName = key
		}
		//比对场景
		if In_array(key,sceneArr){
			sceneStatus=true
			strArr:=strings.Split(str,"|")
			for _,item := range strArr {  //验证单字段
				itemArr := strings.Split(item,":")
				if len(itemArr) == 2{
					item = itemArr[0]
					switch item {
					case "len":
						val , AtoiErr := strconv.Atoi(itemArr[1])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						if len(data[key]) != val{
							err = keyName + "长度不符合规则"
							break I
						}
					case "=":
						if data[key] != itemArr[1]{
							err = keyName + "不符合规则"
							break I
						}
					case "max":
						val , AtoiErr := strconv.Atoi(itemArr[1])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						value ,AtoiErr  :=strconv.Atoi(data[key])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						if value > val{
							err = keyName + "值不符和规则"
							break I
						}
					case ">":
						val , AtoiErr := strconv.Atoi(itemArr[1])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						value ,AtoiErr  :=strconv.Atoi(data[key])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						if value <= val{
							err = keyName + "值不符和规则"
							break I
						}
					case ">=":
						val , AtoiErr := strconv.Atoi(itemArr[1])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						value ,AtoiErr  :=strconv.Atoi(data[key])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						if value < val{
							err = keyName + "值不符和规则"
							break I
						}
					case "<":
						val , AtoiErr := strconv.Atoi(itemArr[1])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						value ,AtoiErr  :=strconv.Atoi(data[key])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						if value >= val{
							err = keyName + "值不符和规则"
							break I
						}
					case "<=":
						val , AtoiErr := strconv.Atoi(itemArr[1])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						value ,AtoiErr  :=strconv.Atoi(data[key])
						if AtoiErr!=nil{
							err = keyName+"字段验证错误"
							break I
						}
						if value > val{
							err = keyName + "值不符和规则"
							break I
						}
					}
				}else{
					//判断单词的验证
					switch item {
					case "require":
						if   data[key] == "" {
							err = keyName + "不能为空"
							break I
						}
					case "mobile":
						match,_:=regexp.MatchString(`^1([38][0-9]|14[57]|5[^4])\d{8}$`,data[key])
						if !match {
							err = "手机号格式有误"
							break I
						}
					case "date":
						_, dateErr := time.Parse("2006-01-02", data[key])
						if dateErr != nil {
							err =  "时间格式有误"
							break I
						}
					case "dateTime":
						_, dateErr := time.Parse("2006-01-02 15:04:05", data[key])
						if dateErr != nil {
							err = "时间格式有误"
							break I
						}
					case "email":
						match,_:=regexp.MatchString(`\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`,data[key])
						if !match {
							err = "邮箱不符合规则"
							break I
						}
					case "ip":
						match,_:=regexp.MatchString(`^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){2}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`,data[key])
						if !match {
							err = "ip地址不符合规则"
							break I
						}
					case "idcard":
						match,_:=regexp.MatchString(`^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$`,data[key])
						if !match {
							err = "身份证号码不符合规则"
							break I
						}
					}
				}
			}
		}
	}
	if !sceneStatus{
		err = "场景不存在"
	}
	return err
}


func StructToMap(obj interface{}) map[string]string {
	obj_v := reflect.ValueOf(obj)
	v := obj_v.Elem()
	typeOfType := v.Type()
	var data = make(map[string]string)
	for i := 0; i < v.NumField(); i++ {
		field := v.Field(i)
		data[typeOfType.Field(i).Name] = field.Interface().(string)
	}
	return data
}

func In_array( item string,items []string) bool {
	for _, eachItem := range items {
		if eachItem == item {
			return true
		}
	}
	return false
}

